package com.gvfish.backingbean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.gvfish.entity.Article;
import com.gvfish.entity.Category;
import com.gvfish.service.ArticleManager;
import com.gvfish.service.CategoryManager;
import com.gvfish.service.ConstantManager;
import com.gvfish.service.LinkManager;

@ManagedBean
@ApplicationScoped
public class Index {
	ApplicationContext ctx = FacesContextUtils
	.getWebApplicationContext(FacesContext.getCurrentInstance());
	LinkManager linkManager = (LinkManager) ctx.getBean("linkManager");
	ArticleManager articleManager = (ArticleManager)ctx.getBean("articleManager");
	ConstantManager constantManager=(ConstantManager)ctx.getBean("constantManager");
	CategoryManager categoryManager = (CategoryManager)ctx.getBean("categoryManager");
	
	
	private Map<String,String> constant	=constantManager.getConstantMap();
	private List<Category> categorys = categoryManager.findAll();
	private List<List<Article>> artList= new ArrayList<List<Article>>();
	private Map<Category,List<Article>> artMap = new HashMap<Category, List<Article>>();
	public void setConstant(Map<String,String> constant) {
		this.constant = constant;
	}

	public Map<String,String> getConstant() {
		return constant;
	}

	public void setCategorys(List<Category> categorys) {
		this.categorys = categorys;
	}

	public List<Category> getCategorys() {
		return categorys;
	}

	public void setArtList(List<List<Article>> artList) {
		this.artList = artList;
	}

	public List<List<Article>> getArtList() {
		if(artList.size()==0){
			for(Category category:categorys){
				artList.add( articleManager.getByCategory(category.getId()));
			}
		}
		return artList;
	}

	public void setArtMap(Map<Category,List<Article>> artMap) {
		this.artMap = artMap;
	}

	public Map<Category,List<Article>> getArtMap() {
		if(artMap.keySet().size()==0){
			for(Category category:categorys){
				artMap.put(category, articleManager.getByCategory(category.getId()));
			}
		}
		return artMap;
	}



}
